package org.i1510.base.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MyBatisProvider {

	public String getAllPropertiesForSql(Object obj,String objName,String tableName) {
		String rtn ="";
		Map<String, Object> map = new HashMap<String, Object>();

		BeanUtils.ObjectCopyMap(obj, map);
		if (null == obj){
			return rtn;
		}
		Iterator it = map.entrySet().iterator();
		StringBuilder fileds = new StringBuilder(" ");
		StringBuilder value = new StringBuilder(" ");
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			fileds.append(entry.getKey().toString().trim()+",");
			value.append("#{" + objName + "." + entry.getKey().toString().trim() + "},");
		}

		// remove last ','
		fileds.deleteCharAt(fileds.length() - 1);
		value.deleteCharAt(value.length() - 1);
		fileds.append(") ");
		value.append(") ");
		
		map.put("fileds", fileds.toString());
		map.put("value", value.toString());

		String sql = "insert into "+tableName;
		System.out.println(sql+fileds.toString() + value.toString());

		return sql+fileds.toString() + value.toString();
	}
	public String getMap(Map mapIn) {
		if(null != mapIn && null != mapIn.get("map")){
			Map map = (Map)mapIn.get("map");
			String rtn ="";;
			
			//获取mbase
			if(null != map.get("mBase") && null != map.get("mTableName")){
				//表名
				String tableName = map.get("mTableName").toString();
				//po对象数据
				Map mBase = (Map)map.get("mBase");
				Iterator it = mBase.entrySet().iterator();
				StringBuilder fileds = new StringBuilder(" (");
				StringBuilder value = new StringBuilder(" (");
				while (it.hasNext()) {
					Map.Entry entry = (Map.Entry) it.next();
					fileds.append(entry.getKey().toString().trim()+",");
					value.append("#{map." + entry.getKey().toString().trim() + "},");
				}

				// remove last ','
				fileds.deleteCharAt(fileds.length() - 1);
				value.deleteCharAt(value.length() - 1);
				fileds.append(") ");
				value.append(") ");

				String sql = "INSERT INTO "+tableName;
				System.out.println(sql+fileds.toString() +"VALUES"+ value.toString());

				return sql+fileds.toString() +"VALUES"+ value.toString();
			}else{
				return "";
			}
			
		}else{
			return "";
		}
		
	}
}
